package com.winhxd.b2c.common.feign.product;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.product.condition.ProductAttributeAndOptionCondition;
import com.winhxd.b2c.common.domain.product.condition.ProductAttributeCondition;
import com.winhxd.b2c.common.domain.product.model.ProductAttribute;
import com.winhxd.b2c.common.domain.product.vo.ProductAttributeAndOptionVO;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author cuipengbo
 * @create 2019-01-07 11:21
 * @desc 商品属性管理
 **/
@FeignClient(value = ServiceName.PRODUCT_SERVICE, fallbackFactory = AttributeManagementServiceFallback.class)
public interface AttributeManagementServiceClient {


    /**
     * 查询商品属性列表
     * @param
     * @return
     */
    @RequestMapping(value = "/attributeManagement/8410/v1/findProductAttributeList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<PagedList<ProductAttribute>> findProductAttributeList(@RequestBody ProductAttributeCondition productAttributeCondition);

    /**
     * 修改商品属性信息
     * @param productAttributeCondition
     * @return
     */
    @RequestMapping(value = "/attributeManagement/8411/v1/modifyProductAttributeById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Integer> modifyProductAttributeById(@RequestBody ProductAttributeCondition productAttributeCondition);

    /**
     *  根据属性id查询属性信息(编辑回显)
     * @param productAttributeCondition
     * @return
     */
    @RequestMapping(value = "/attributeManagement/8412/v1/getProductAttributeById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<ProductAttributeAndOptionVO> getProductAttributeById(@RequestBody ProductAttributeCondition productAttributeCondition);

    /**
     * 保存属性信息
     * @param condition
     * @return
     */
    @RequestMapping(value = "/attributeManagement/8413/v1/saveOrUpdateProductAttribute", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Integer> saveOrUpdateProductAttribute(@RequestBody ProductAttributeAndOptionCondition condition);

    /**
     * 根据条件查询商品属性信息
     * @param productAttribute
     * @return
     */
    @RequestMapping(value = "/attributeManagement/8414/v1/findProductAttributeByModel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<List<ProductAttribute>> findProductAttributeByModel(@RequestBody ProductAttributeCondition productAttribute);
}

@Component
class AttributeManagementServiceFallback implements FallbackFactory<AttributeManagementServiceClient> {
    private static final Logger logger = LoggerFactory.getLogger(AttributeManagementServiceFallback.class);

    @Override
    public AttributeManagementServiceClient create(Throwable throwable) {
        return new AttributeManagementServiceClient(){
            @Override
            public ResponseResult<PagedList<ProductAttribute>> findProductAttributeList(ProductAttributeCondition productAttributeCondition) {
                logger.error("AttributeManagementServiceClient -> findProductAttributeList", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }

            @Override
            public ResponseResult<Integer> modifyProductAttributeById(ProductAttributeCondition productAttributeCondition) {
                logger.error("AttributeManagementServiceClient -> modifyProductAttributeById", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }

            @Override
            public ResponseResult<ProductAttributeAndOptionVO> getProductAttributeById(@RequestBody ProductAttributeCondition productAttributeCondition) {
                logger.error("AttributeManagementServiceClient -> getProductAttributeById", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }

            @Override
            public ResponseResult<Integer> saveOrUpdateProductAttribute(ProductAttributeAndOptionCondition condition) {
                logger.error("AttributeManagementServiceClient -> saveOrUpdateProductAttribute", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }

            @Override
            public ResponseResult<List<ProductAttribute>> findProductAttributeByModel(ProductAttributeCondition productAttribute) {
                logger.error("AttributeManagementServiceClient -> findProductAttributeByModel", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }
        };
    }

}
